package com.sinoglobal.sinostore.activity;

import java.util.ArrayList;

import andbase.listener.AbOnListViewListener;
import andbase.view.pullview.AbPullListView;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import com.alibaba.fastjson.JSON;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.sinoglobal.sinostore.BaseActivity;
import com.sinoglobal.sinostore.R;
import com.sinoglobal.sinostore.StateBaseActivity;
import com.sinoglobal.sinostore.adapter.ShopWareCommentAdapter;
import com.sinoglobal.sinostore.bean.CommentListVo;
import com.sinoglobal.sinostore.bean.CommentVo;
import com.sinoglobal.sinostore.system.Constants;
import com.sinoglobal.sinostore.utils.ApiDebugUtil;

/**
 * 
* @title: EvaluationListActivity.java 
* @package com.chiliring.sinostore.activity 
* @description: TODO(评价列表) 
* @author LK
* @date 2015-1-15 上午9:55:26
 */
public class EvaluationListActivity extends StateBaseActivity implements AbOnListViewListener {
	private int pagenum = 0; //起始位置
	private int limit2 = 10; //数据长度
	private boolean onRefreshflag = true; // 刷新标识
	private boolean onLoadMoreflag = true; // 加载更多标识
	private AbPullListView shopListView; // 评论的listview
	private String shop_Ware_id; // 商品id
	private ShopWareCommentAdapter shopWareCommentAdapter;
	private View shopDetailNodata;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		titleView.setText("评价");
		setContentView(R.layout.shop_activity_evaluation_list);
		openChenJInShi();
		initView();
		
		initData();
	}

	private void initView(){
		shopListView = (AbPullListView) findViewById(R.id.evaluation_list);
		shopDetailNodata = (View) findViewById(R.id.no_evaluation_layout);
		shopWareCommentAdapter = new ShopWareCommentAdapter(this,new ArrayList<CommentVo>());
		shopListView.setAdapter(shopWareCommentAdapter);
		shopListView.setAbOnListViewListener(this);
	}
	
	private void initData(){
		shop_Ware_id = getIntent().getStringExtra("goodsId");
		loadInfo();
	}
	
	/**
	 * 
	* @author LK
	* @methods loadInfo 
	* @description 描述信息  加载数据
	* @date 2015-1-15 下午12:43:21
	* @param type 参数说明
	 */
	private void loadInfo() {
		final ProgressDialog dialog = new ProgressDialog(this);
		try {
			dialog.show();
		} catch (Exception e) {
		}
		RequestParams params = new RequestParams();
		params.addQueryStringParameter("por", "104");
		params.addQueryStringParameter("proIden", Constants.STOREFLAG);
		params.addQueryStringParameter("limit1", String.valueOf(pagenum));
		params.addQueryStringParameter("limit2", String.valueOf(limit2));
		params.addQueryStringParameter("id", shop_Ware_id);
		params.addQueryStringParameter("type", Constants.COMMENT_CODE);
		ApiDebugUtil.debug(params);
		HttpUtils httpUtils = new HttpUtils();
		httpUtils.send(HttpMethod.POST, Constants.STORE_URL, params, new RequestCallBack<String>() {
			@Override
			public void onFailure(HttpException arg0, String arg1) {
				try {
					dialog.dismiss();
				} catch (Exception e) {
				}
			}

			@Override
			public void onSuccess(ResponseInfo<String> responseInfo) {
				try {
					dialog.dismiss();
				} catch (Exception e) {
				}
				String result = responseInfo.result;
				if(!TextUtils.isEmpty(result)){
					CommentListVo entity;
					try {
						entity = JSON.parseObject(result, CommentListVo.class);
					} catch (Exception e) {
						e.printStackTrace();
						showShortToast("数据解析错误");
						return;
					}
					if(Constants.STORE_SUCCESS_CODE.equals(entity.getCode())){
						if (pagenum == 0 &&  entity.getJson().size() == 0) {
							shopDetailNodata.setVisibility(View.VISIBLE);
							shopListView.setVisibility(View.GONE);
							//无数据的操作
						} else {
							shopListView.setVisibility(View.VISIBLE);
							if(pagenum == 0){
								shopWareCommentAdapter.clearData();
							}
							shopWareCommentAdapter.AddData(entity.getJson());
							
							if (entity.getJson().size() < 10) {
								shopListView.setPullLoadEnable(false);
							}
							}
							onLoadMoreflag = true;
							onRefreshflag = true;
							shopListView.stopRefresh();
							shopListView.stopLoadMore();
						}
					}
			}
		});

	}



	@Override
	protected void onDestroy() {
		super.onDestroy();
	}

	@Override
	public void onRefresh() {
		if (onRefreshflag) {
			pagenum = 0;
			loadInfo();
			onRefreshflag = false;
		}
	}

	@Override
	public void onLoadMore() {
		if (onLoadMoreflag) {
			pagenum += limit2;
			loadInfo();
			onLoadMoreflag = false;
		}
	}
}
